package com.hejia.alauda.persistence.mybatis;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.hejia.alauda.entity.Interest;
import com.hejia.alauda.persistence.BaseMapper;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.vos.InterestVo;

public interface InterestMapper extends BaseMapper<Interest> {
	
	Float findInterestByDay(Map<String, Object> params);
	
	List<Map<String, Object>> findInterestList(@Param("params") Map<String, Object> params,@Param("pager") Pager pager);
	
	Integer findCountInterestList(@Param("params") Map<String, Object> params);
	
	List<Map<String, Object>> findCurrentInterestList(@Param("params") Map<String, Object> params,@Param("pager") Pager<Map<String, Object>> pager);
	
	Integer findCurrentInterestListCount(@Param("params") Map<String, Object> params);
	
	String findInterestSum(@Param("params") Map<String, Object> params);
	
	@Insert("INSERT INTO `interest`(`createDate`,`modifyDate`,`financing_id`,`interest`,`income`,`valueDate`,`calculated`, `user_id`) VALUES(now(),now(),#{financing_id},#{interest},#{income},#{valueDate},#{calculated},#{user_id})")
	int insertInterest(Map<String, Object> params);
	
	List<Map<String,Object>> findWholeInterestList(Map<String,Object> params);
	
	/**
	 * 分页查询每天利息总和，记录数量
	 * 
	 * @param params 查询参数
	 * 
	 * @param pager  分页条件
	 * @return
	 */
	List<InterestVo> selectInterestlistByDaily(@Param("params") Map<String,Object> params, @Param("pager") Pager pager);
	/**
	 * 按天分组查询 页数
	 * @param params
	 * @return
	 */
	int selectInterestlistByDailyWithCount(@Param("params") Map<String,Object> params);
	
	/**
	 * 根据日期查询用户当天的收益详情
	 * 
	 * @param params
	 * @return
	 */
	List<Interest> selectInterestByValueDate(@Param("valueDate") Date valueDate, @Param("userId") Long userId);
	
	/**
	 * 根据计息时间分组获取用户单日最高收益信息
	 * 
	 * @param userId 用户ID
	 * @return
	 */
	@Select("SELECT `valueDate` valueDate, SUM(IFNULL(interest,0)) incomes from interest where user_id = #{userId} GROUP BY valueDate ORDER BY interest desc  LIMIT 0,1")
	Map<String, Object> selectUserMaxInterestByDay(@Param("userId") Long userId);
	
	/**
	 * 根据日期查询当天收益详情
	 * 
	 * @param valueDate 计息时间
	 * @return
	 */
	@Select("SELECT i.interest,f.name FROM interest i LEFT JOIN financing f ON i.financing_id = f.id WHERE i.user_id = #{params.user_id} AND i.valueDate = #{params.valueDate}")
	List<Map<String, Object>> selectInterestDay(@Param("params") Map<String,Object> params);
	
}
